const Joi = require('joi');

class HomeValidate {
  async list(param) {
    const schema = Joi.object({
      name: Joi.string(),
      model: Joi.string(),
      url: Joi.string(),
      reqmethod: Joi.string(),
      type: Joi.string(),
      order: Joi.string().default('create_time'),
      method: Joi.string().default('desc'),
      pageIndex: Joi.string().default('1'),
      pageSize: Joi.string().default('10'),
    });

    try {
      const value = await schema.validateAsync(param);
      return value;
    } catch (error) {
      throw error;
    }
  }

  async insert(param) {
    const schema = Joi.object({
      model: Joi.string()
        .required()
        .messages({
          'string.empty': '模块不能为空',
          'string.required': '模型是必填项',
        }),

      url: Joi.string()
        .required()
        .messages({
          'string.empty': '请求路径不能为空',
          'string.required': '请求路径是必填项',
        }),

      ip: Joi.string()
        .required()
        .messages({
          'string.empty': 'ip不能为空',
          'string.required': 'ip是必填项',
        }),

      method: Joi.string()
        .required()
        .messages({
          'string.empty': '请求方式不能为空',
          'string.required': '请求方式是必填项',
        }),

      param: Joi.object()
        .required()
        .default({}),

      result: Joi.object()
        .required()
        .default({}),

      type: Joi.number()
        .required()
        .messages({
          'number.empty': '类型不能为空',
          'number.required': '类型是必填项',
        }),
    });

    try {
      const value = await schema.validateAsync(param);

      return value;
    } catch (error) {
      throw error;
    }
  }
}

module.exports = new HomeValidate();
